﻿using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;

namespace ServiceCore.Tool.MongoDB
{
    public interface IMongoDB_Config
    { 
        IMongoDatabase database { get; }
    }
    public class MongoDB_Config : IMongoDB_Config
    {
        private readonly IConfiguration AppSettingConfig;
        public MongoDB_Config(IConfiguration configuration)
        {
            AppSettingConfig = configuration;

        }
        public string mongodb_url { get { return AppSettingConfig["mongodb_conn:url"]; } }
        public string mongodb_dbname { get { return AppSettingConfig["mongodb_conn:dbname"]; } }
        private static MongoClient _mongoDB_Client;
        private MongoClient MongoClient
        {
            get
            {
                if (_mongoDB_Client == null)
                {
                    _mongoDB_Client = new MongoClient(mongodb_url + mongodb_dbname);
                }
                return _mongoDB_Client;
            }
        } 
        public static IMongoDatabase _database;
        public IMongoDatabase database
        {
            get
            {
                if (_database == null)
                {
                    _database = MongoClient.GetDatabase(mongodb_dbname);
                }
                return _database;
            }
        }
    }
}
